package cn.daijiahui.loginregistdaijiahui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    EditText username;
    EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        username= (EditText) findViewById(R.id.edUserName);
        password= (EditText) findViewById(R.id.edPassword);
    }
    //登陆
    public void setLogin(View view) {
        //获取Edit内容
        String user=username.getText().toString();
        String pass=password.getText().toString();
        //使用SharedPreferences读取账号和密码
        SharedPreferences share=getSharedPreferences("data", MODE_PRIVATE);
        SharedPreferences.Editor editor=share.edit();
        String user1=share.getString("name","");
        String pass1=share.getString("password","");
        boolean a=user.equals(user1);
        boolean b=pass.equals(pass1);
        Log.e(TAG, "setLogin: "+a+"//"+b+"//"+user+"//"+user1+"//"+pass+"//"+pass1 );
        //判断账号和密码是否正确
        if (a&&b){
            //正确，跳转到用户界面
            Intent intent=new Intent();
            intent.setClass(MainActivity.this,UserActivity.class);
            MainActivity.this.startActivity(intent);

        }else {
            Toast.makeText(this,"账号或密码错误",Toast.LENGTH_LONG).show();
        }

    }
    //跳转注册页面
    public void setRegister(View view) {
        //使用Intent跳转
        Intent intent=new Intent();
        intent.setClass(MainActivity.this,RegisterActivity.class);
        MainActivity.this.startActivity(intent);
    }
}
